<?php

namespace Admin\Controller;

use Common\Model\InquiryModel;
use Admin\Controller\AiController;

class adminController extends AiController
{

    #登录页

    public function login()
    {

        if (IS_POST) {

            $username = trim(I('username'));

            $password = trim(I('password'));

            $admin_info = D('Admin')->login($username, $password);
            
             
            if ($admin_info) {
                
                

                if ($admin_info['state'] == 0) $this->error('您的账号处于锁定状态,不允许登录');

                session('ADMIN_TYPE', $admin_info['admin_t'], 86400);

                session('ADMIN_ID', $admin_info['id'], 86400);
              
                session('ADMIN_NAME', $admin_info['username'], 86400);
                session('ADMIN_KEY', md5($admin_info['admin_t'] . $admin_info['id'] . $admin_info['username'] . parent::cookie_key), 86400);

                //记录系统管理员日志
                D('AdminLog')->addLog(array('controller' => 'AdminController', 'action' => 'login', 'content' => '登录了系统'));
                
            

                $this->redirect('Admin/center');

            } else {

                $this->error('账号或密码错误');

            }

        } else {

            $this->display();

        }


    }



    public function center() {


        // 私人医生
//        $inquiry = new InquiryModel();
//        $last_id = $inquiry->getLastId();
//        $this->assign('last_id', $last_id);

        $this->display();

    }


    public function manager_top()
    {


        $admin_type = session('ADMIN_TYPE');
         
     

        if ($admin_type == '1') {//如果是超级管理员则不进行条件查询

            $where1 = '';

        } else {

            $where1 = 'role_id=' . $admin_type;

        }

        //角色权限控制
  
        $res = D('Role')->where($where1)->find();
        $ids = $res['role_auth_ids'];
        $where = " admin_name = 'Admin' ";
        if ($admin_type == '1') {

            $where .= ' AND auth_level=0 ';

        } else {

            $where .= " AND auth_id IN ($ids) AND auth_level=0 ";

        }

        $auth = D('Auth')->where($where)->select();

        foreach ($auth as $key => $val) {

            $auth[$key]['url'] = URL('Admin/manager_left?auth_pid=' . $val['auth_id']);

        }

        $this->assign('auth', $auth);
        
        $this->display();
    }


    public function manager_left()
    {


        $admin_type = session('ADMIN_TYPE');

        $auth_pid = I('auth_pid');

        $where1 = 'role_id=' . $admin_type;

        $res = D('Role')->where($where1)->find();

        $ids = $res['role_auth_ids'];

        if (!$auth_pid) {

            //查询出当前auth_pid

            $arr = field2ids($ids);

            $auth_pid = $arr[0];

        }

        if ($admin_type == '1') {

            $where2 = " auth_pid = $auth_pid AND auth_level=1 ";

        } else {

            $where2 = " auth_id IN ($ids) AND auth_pid = $auth_pid AND auth_level=1 ";

        }


        //查询二级分类

        $auth1 = D('Auth')->field('auth_id,auth_name')->where($where2)->select();

        if (!$auth1) {

            exit('暂无权限，请添加');

        }

        $str = '';

        foreach ($auth1 as $key => $val) {

            $str .= $val['auth_id'] . ',';

        }

        $str = rtrim($str, ',');

        if ($admin_type == '1') {

            $where2 = " auth_level=2 AND auth_pid IN($str)";

        } else {

            $where2 = " auth_id IN ($ids) AND auth_level=2 AND auth_pid IN($str)";

        }


        $auth2 = D('Auth')->where($where2)->select();


        foreach ($auth2 as $key => $val) {

            $auth_c = $val['auth_c'];

            $auth_a = $val['auth_a'];

            $auth2[$key]['url'] = URL("$auth_c/$auth_a");

        }


        $this->assign('auth1', $auth1);

        $this->assign('auth2', $auth2);

        $this->display();

    }


    public function manager_foot()
    {

        $this->display();

    }


}